import { _decorator, Animation, Component, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, FSM_PARAM_TYPE_ENUM, PARAMS_NAME_ENUM } from '../Enum';
const { ccclass } = _decorator;
import { IState, SubMachine } from './State';


@ccclass('StateMachine')
export class StateMachine<TKey> extends Component {
  mainMachine:SubMachine<TKey> = new SubMachine();
  animationComponent: Animation;
  waitingList: Array<Promise<SpriteFrame[]>> = [];
  defaultState: ENTITY_STATE_ENUM;  

  get currState(): IState<TKey> {
      return this.mainMachine.currState;
  }

  startWith(name: TKey) {
      this.mainMachine.defaultState = name;
      this.mainMachine.transitTo(name);
  }

  registerState(state: IState<TKey>) {
      this.mainMachine.add(state);
  }

  deregisterState(name: TKey) {
      this.mainMachine.remove(name);
  }

  transit(name: TKey) {
      // console.info(this.currState.id+" => "+name);
      this.mainMachine.transitTo(name);
  }

  update(dt: number) {
      this.mainMachine.update(dt);
  }

}